package com.adan.auth.service.impl;

import com.adan.auth.mapper.XcUserMapper;
import com.adan.auth.model.dto.CheckCodeDto;
import com.adan.auth.model.po.XcUser;
import com.adan.auth.service.VerifyCodeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 修改密码
 * @date: 2023-06-24 23:47
 */
@Service
public class VerifyCodeServiceImpl implements VerifyCodeService {

    @Autowired
    StringRedisTemplate redisTemplate;
    @Resource
    private XcUserMapper xcUserMapper;
    @Resource
    private VerifyCodeServiceImpl currentProxy;

    @Override
    public boolean updatePassword(CheckCodeDto checkCodeDto) {

        // 判断验证码
        String checkcode = checkCodeDto.getCheckcode();
        String check = redisTemplate.opsForValue().get(checkcode);
        if (check == null) {

            throw new RuntimeException("验证码出错");
        }
        // 校验两次输入密码是否一致
        String password = checkCodeDto.getPassword();
        String confirmpwd = checkCodeDto.getConfirmpwd();
        if (!password.equals(confirmpwd)) {
            throw new RuntimeException("输入密码不一致出错");
        }
        // 校验手机/邮箱
        String email = checkCodeDto.getEmail();
        String cellphone = checkCodeDto.getCellphone();

        if (isEmail(check)) {
            // 处理邮箱
            if (!email.equals(check)) {
                throw new RuntimeException("邮箱信息出错");
            }
            // 根据邮箱查询数据库，修改密码
            XcUser user = xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>()
                    .eq(XcUser::getEmail, email));
            currentProxy.updatePassword(password, user);
        } else if (isPhone(check)) {
            // 处理手机
            if (!cellphone.equals(check)) {
                throw new RuntimeException("手机信息出错");
            }
            // 根据手机号查询数据库，修改密码
            // 根据邮箱查询数据库，修改密码
            XcUser user = xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>()
                    .eq(XcUser::getCellphone, cellphone));
            currentProxy.updatePassword(password, user);
        } else {
            // 字符串既不是邮箱也不是手机
            throw new RuntimeException("手机/邮箱格式信息出错");
        }
        return true;
    }

    @Transactional
    public void updatePassword(String password, XcUser user) {
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        user.setPassword(new BCryptPasswordEncoder().encode(password));
        xcUserMapper.updateById(user);
    }


    public static boolean isEmail(String str) {
        Pattern p = Pattern.compile("^\\w+@(\\w+\\.)+[a-z]{2,3}$");
        Matcher m = p.matcher(str);
        return m.matches();
    }

    public static boolean isPhone(String str) {
        Pattern p = Pattern.compile("^1[0-9]{10}$");
        Matcher m = p.matcher(str);
        return m.matches();
    }

}
